/**
 * alarmmsg.c
 */

#include <stdlib.h>
#include <string.h>

#include "../alarmCommon.h"
#include "alarmMsg.h"

void timeToBuff(char *pTimeBuff, struct tm *pTm) {
	pTimeBuff[0] = pTm->tm_year / 100 + 19;	// NOTE: tm_year is begin from 1900
	pTimeBuff[1] = pTm->tm_year % 100;
	pTimeBuff[2] = pTm->tm_mon + 1;	//NOTE: tm_mon is 0 - 11
	pTimeBuff[3] = pTm->tm_mday;
	pTimeBuff[4] = pTm->tm_hour;
	pTimeBuff[5] = pTm->tm_min;
	pTimeBuff[6] = pTm->tm_sec;
}

AlarmMsg *genAlarmMsg(char *pBuff) {
	int msgSize = sizeof(AlarmMsg);
	AlarmMsg *pMsg = (AlarmMsg *) malloc(msgSize);
	if (pMsg != NULL) {
		memcpy(pMsg, pBuff, msgSize);
	}

	return pMsg;
}

int alarmMsgToBuff(char *pBuff, int buffSize, AlarmMsg *pMsg) {
	int needSize = sizeof(AlarmMsg);
	if (needSize > buffSize) {
		return -1;
	}

	memcpy(pBuff, pMsg, needSize);

	return needSize;
}

